
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: '../../service/admin/getMemberMeasurement',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapMemberMeasurementToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#AdMemberMeasurement_Add', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Đăng kí số đo mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#AdMemberMeasurementCommon_Modified', 'click', function () {
        toEditPage();
    });
}

function sendInfo() {
    var measurement = {};

    measurement.maSDCoThe = ISS.DOM.getVal('#AdMemberMeasurement_code');
    measurement.sdNguc = ISS.DOM.getVal('#AdMemberMeasurement_breast');
    measurement.sdEo = ISS.DOM.getVal('#AdMemberMeasurement_waist');
    measurement.sdMong = ISS.DOM.getVal('#AdMemberMeasurement_butt');
    measurement.sdDaiLung = ISS.DOM.getVal('#AdMemberMeasurement_back');
    measurement.sdDaiTay = ISS.DOM.getVal('#AdMemberMeasurement_arm');
    measurement.sdVai = ISS.DOM.getVal('#AdMemberMeasurement_shoulder');
    measurement.sdChieuCao = ISS.DOM.getVal('#AdMemberMeasurement_height');
    measurement.sdCanNang = ISS.DOM.getVal('#AdMemberMeasurement_weight');

    ISS.BACKEND.request({
        url: '../../service/admin/header/adMemberMeasurement',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(measurement),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function toEditPage() {
    var _maSDCoThe = []
    ISS.DOM.forEach('#form-AdMemberMeasurementCommon input[type="checkbox"]:checked', function(){
        _maSDCoThe.push($(this).val());
    });

    if(_maSDCoThe.length == 1) {
        var _url = '/service/admin/toMemberMeasurementPage/' + _maSDCoThe[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Không được chọn hơn một dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdMemberMeasurement_code', 'error');
    ISS.DOM.removeClass('#AdMemberMeasurement_breast', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapMemberMeasurementToView(measurement) {
    var rows = measurement.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Mã số đo cơ thể (cm)</th>';
    str += '       <th class="columnRule">SĐ Ngực (cm)</th>';
    str += '       <th class="columnRule">SĐ Eo (cm)</th>';
    str += '       <th class="columnRule">SĐ Mông (cm)</th>';
    str += '       <th class="columnRule">SĐ Dài Lưng (cm)</th>';
    str += '       <th class="columnRule">SĐ Dài Tay (cm)</th>';
    str += '       <th class="columnRule">SĐ Vai (cm)</th>';
    str += '       <th class="columnRule">SĐ Chiều Cao (cm)</th>';
    str += '       <th class="columnRule">Cân Nặng (KG)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">';
        }
        else
        {
            str += '<tr class="column-Light">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + measurement[i].maSDCoThe + '" type="checkbox" value="' + measurement[i].maSDCoThe + '"/></td>';
            str += '<td class="columnRule">' + measurement[i].maSDCoThe + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdNguc + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdEo + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdMong + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdDaiLung + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdDaiTay + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdVai + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdChieuCao + '</td>';
            str += '<td class="columnRule">' + measurement[i].sdCanNang + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#AdMemberMeasurementCommon_All', str);
}